﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Meqs.Entity;
using System.Web.Services;
using Meqs.Logic.Interface;
using NPOI.HSSF.UserModel;
using System.IO;
using System.Reflection;
using NPOI.HSSF.Util;
public partial class Credit_Teacher_ScoreInput_luofang_ExamScoreInput : System.Web.UI.Page
{
    /// <summary>
    /// 显示录入
    /// </summary>
    protected string title = "初一一班 语文 期末成绩录入";
    /// <summary>
    /// 班级信息
    /// </summary>
    protected ClsInfo cls = null;
    /// <summary>
    /// 课程信息
    /// </summary>
    protected GlobalCauseInfo cause = null;
    protected ClassResultExamList cresultList = null;
    /// <summary>
    /// 学生列表信息
    /// </summary>
    protected IList<StudentInfo> studentInfo = null;
    /// <summary>
    /// 成绩信息
    /// </summary>
    protected IList<StuExamDetail> stuExamDetail = null;

    /// <summary>
    /// 校考列表
    /// </summary>
    protected IList<ExamsSchool> examschool = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //http://localhost:7130/Meqs.Login/Credit/Teacher/scoreinput/luofang/ExamScoreInput.aspx?classid=12&teacherid=8&causeid=1
            int classid = Convert.ToInt32(Request.QueryString["classid"]);
            //int teacherid = Convert.ToInt32(Request.QueryString["teacherid"]);
            int causeid = Convert.ToInt32(Request.QueryString["causeid"]);
            //int classid = Convert.ToInt32("12");
            ////int teacherid = Convert.ToInt32("8");
            //int causeid = Convert.ToInt32("1");
            cls = Container<IClsInfoLogic>.Instance.FindById(classid);
            cause = Container<IGlobalCauseInfoLogic>.Instance.FindById(causeid);
           
            if (cls != null && cause != null)
            {
                title = cls.ClsName + "  " + cause.CauseName + "  期末成绩录入";

                Hashtable table = new Hashtable();
                table.Add("ClsID", classid);
                table.Add("IsEffect", 1);
                studentInfo = Container<Meqs.Logic.Interface.IStudentInfoLogic>.Instance.FindAll(table);
                
                table = new Hashtable();
                table.Add("ClassID", classid);
                table.Add("SubjectID", causeid);
                table.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
                table.Add("SchoolTerm", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
                table.Add("SchoolMark", SchoolCommon.Sch_SchoolInfo.SchoolMark);
                examschool = Container<IExamsSchoolLogic>.Instance.FindAll(table);

                table = new Hashtable();
                table.Add("SchoolMark", CommonScore.GetSchoolMark);
                table.Add("ClassID", classid);
                table.Add("CauseID", causeid);
                table.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
                table.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
                IList<ClassResultExamList> clist = Container<Meqs.Logic.Interface.IClassResultExamListLogic>.Instance.FindAll(table);
                if (clist.Count > 0)
                {
                    cresultList = clist[0];
                    table = new Hashtable();
                    table.Add("SchoolMark", CommonScore.GetSchoolMark);
                    table.Add("ClassID", classid);
                    table.Add("CauseID", causeid);
                    table.Add("TypeID", clist[0].RID);
                    stuExamDetail = Container<Meqs.Logic.Interface.IStuExamDetailLogic>.Instance.FindAll(table);
                    
                }
                else
                {
                    stuExamDetail = new List<StuExamDetail>();

                }
                Repeater1.DataSource = studentInfo;
                Repeater1.DataBind();
            }
            
            
        }
    }
    protected string GetHeader()
    {
        string header = "<th width='80px' style='text-align:left;padding-left: 5px;'>学号</th><th width='80px' style='text-align:left;padding-left: 5px;'>姓名</th>";

        header += "<th width='" + 160 + "px' style='text-align:right;padding-right: 5px;'>" + cause.CauseName + "</th>";
     
        return header;
    }


    /// <summary>
    /// 够找一个学生的考试成绩
    /// </summary>
    /// <param name="stuid"></param>
    /// <returns></returns>
    protected string GetStudent(int stuid, string StudentCode, string StudentName)
    {
        string header = "<tr  style='background-color: White;' id=" + stuid + " StudentCode=" + StudentCode + " StudentName= " + StudentName + " {scoreObj}>";
        string scoreObj = " stuid=" + stuid + " ";
        header += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + StudentCode + "</td><td width='80px' style='text-align:left;padding-left: 5px;'>" + StudentName + "</td>";

        string score = GetKaoShiScore(stuid);
        scoreObj += " subject" + cause.RID+ "=" + score + " ";
        header += "<td width='" + 160 + "px' style='text-align:right;padding-left: 5px;padding-right: 5px;'><input nextid='" + GetNextId(cause.RID, stuid, StudentCode) + "' name='subject" + cause.RID + "' id='subject" + cause.RID + "_" + StudentCode + "'  maxlength='6' class='txtStyle' style='text-align: right; border-left: 0px; width:95%;border-top: 0px; border-right: 0px; border-bottom: 0px;'  value='" + (score == "null" ? "" : score) + "' oldvalue='" + (score == "null" ? "" : score) + "' onkeypress='return OnKeyPressSecond(event.keyCode,this);' onpaste='return false;' ondragenter='return false;' onkeydown='movefocus(this,event,\"" + GetNextId(cause.RID, stuid, StudentCode) + "\")'  onfocusout='optionSettingScore(this)' onclick='modi(this)' readonly /></td>";


        header += "</tr>";
        header = header.Replace("{scoreObj}", scoreObj);
        return header;
    }
    /// <summary>
    /// 获得某学生的统考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <returns></returns>
    private string GetKaoShiScore(int stuId)
    {

        foreach (StuExamDetail g in stuExamDetail)
        {
            if (g.StudentID == stuId )
            {
                return (g.Score == null) ? "null" : g.Score.ToString();
            }
        }
        return "null";
    }
    private string GetNextId(int subjid, int stuid, string stuCode)
    {
        for (int index = 0; index < studentInfo.Count; index++)
        {
            if (stuid == studentInfo[index].RID && index < studentInfo.Count - 1)
            {
                return "subject" + subjid + "_" + studentInfo[index + 1].StudentCode;
            }
        }
        return "";
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {
        int classid = Convert.ToInt32(Request.QueryString["classid"]);
        int causeid = Convert.ToInt32(Request.QueryString["causeid"]);
        cls = Container<IClsInfoLogic>.Instance.FindById(classid);
        cause = Container<IGlobalCauseInfoLogic>.Instance.FindById(causeid);
        if (cls!=null)
        {
            title = cls.ClsName + "  " + cause.CauseName + "  期末成绩录入";
        }
        cause = Container<IGlobalCauseInfoLogic>.Instance.FindById(causeid);
        Hashtable table = new Hashtable();
        table.Add("SchoolMark", CommonScore.GetSchoolMark);
        table.Add("ClassID", classid);
        table.Add("CauseID", causeid);
        table.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        table.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        IList<ClassResultExamList> clist = Container<Meqs.Logic.Interface.IClassResultExamListLogic>.Instance.FindAll(table);
        table = new Hashtable();
        table.Add("ClsID", classid);
        table.Add("IsEffect", 1);
        studentInfo = Container<Meqs.Logic.Interface.IStudentInfoLogic>.Instance.FindAll(table);
        if (clist.Count > 0)
        {
            cresultList = clist[0];
            table = new Hashtable();
            table.Add("SchoolMark", CommonScore.GetSchoolMark);
            table.Add("ClassID", classid);
            table.Add("CauseID", causeid);
            table.Add("TypeID", clist[0].RID);
            stuExamDetail = Container<Meqs.Logic.Interface.IStuExamDetailLogic>.Instance.FindAll(table);

        }
        else
        {
            stuExamDetail = new List<StuExamDetail>();

        }
        Repeater1.DataSource = studentInfo;
        Repeater1.DataBind();
    }

    [WebMethod]//KindSettingsEducation[] kinds, int examid, int subjectid
    public static string GetExcel(string[] stunumbers, string[] stuNames, int[] subjectids, string[] subjectNames, double[] scores, int clsid)
    {
        string name = SaveExcel(stunumbers, stuNames, subjectids, subjectNames, scores, clsid);
        return name;
    }
    [WebMethod]
    public static ExamDetailSchool[] GetExamScores(int examtype,int examid,int []subjectids,int classid)
    {
        if (examtype == 0)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", CommonScore.GetSchoolMark);
            table.Add("ExamID", examid);
            table.Add("SubjectID", subjectids[0]);
            table.Add("ClsID", classid);
            IList<ExamDetailSchool> temp = Container<IExamDetailSchoolLogic>.Instance.FindAll(table);
            ExamDetailSchool[] edss = new ExamDetailSchool[temp.Count];
            for(int i=0;i<temp.Count;i++)
            {
                edss[i] =  temp[i];
            }
            return edss;
        }
        return new ExamDetailSchool[0];
    }
    /// <summary>
    /// 保存学生分数
    /// </summary>
    /// <param name="stuids">学生id列表</param>
    /// <param name="subjectids">科目列表</param>
    /// <param name="scores">分数列表</param>
    /// <param name="emid">考试id</param>
    /// <param name="eem">是否是统考</param>
    /// <param name="clsid">班级id</param>
    /// <param name="clsType">文理分科</param>
    /// <param name="g"></param>
    /// <returns></returns>
    [WebMethod]//KindSettingsEducation[] kinds, int examid, int subjectid
    public static string SaveStudentScores(int[] stuids, int[] subjectids, double[] scores,  int clsid)
    {
        ((IStuExamDetailLogic)(Container<IStuExamDetailLogic>.Instance)).SaveStudentScores(stuids, subjectids, scores, clsid, SchoolCommon.Sch_SchoolInfo, CommonScore.GetUserID,2);

        return "";
    }


    private static string SaveExcel(string[] stunumbers, string[] stuNames, int[] subjectids, string[] subjectNames, double[] scores,  int clsid)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../../") + "\\download";//生成的文件存放路径
        HSSFWorkbook wb = new HSSFWorkbook();

        ClsInfo ci = ((IClsInfoLogic)Container<IClsInfoLogic>.Instance).FindById(clsid);
        HSSFCellStyle style = wb.CreateCellStyle();
        style.BorderLeft = (short)1;
        style.BorderRight = (short)1;
        style.BorderTop = (short)1;
        style.BorderBottom = (short)1;
        //style.BorderTop=HSSFCellStyle.BORDER_DOUBLE;
        style.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
        style.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
        style.Alignment = HSSFCellStyle.ALIGN_CENTER;
        HSSFSheet sheet = wb.CreateSheet(ci.ClsName + "成绩导入");
        HSSFRow row = sheet.CreateRow((short)0);
        HSSFCell cell = row.CreateCell((short)0);
        cell.SetCellValue("学号");
        cell.SetCellType(1);
        cell.CellStyle = style;

        row = sheet.CreateRow((short)0);
        cell = row.CreateCell((short)1);
        cell.SetCellValue("姓名");
        cell.SetCellType(1);
        cell.CellStyle = style;
        for (int i = 0; i < subjectNames.Length; i++)
        {
            row = sheet.CreateRow((short)0);
            cell = row.CreateCell((short)(i + 2));
            cell.SetCellValue(subjectNames[i]);
            cell.SetCellType(1);
            cell.CellStyle = style;
        }

        for (int i = 0; i < stunumbers.Length; i++)
        {
            HSSFCellStyle style1 = wb.CreateCellStyle();
            style1.BorderLeft = (short)1;
            style1.BorderRight = (short)1;
            style1.BorderTop = (short)1;
            style1.BorderBottom = (short)1;
            style1.Alignment = HSSFCellStyle.ALIGN_LEFT;

            row = sheet.CreateRow((short)(i + 1));
            cell = row.CreateCell((short)0);
            cell.SetCellValue(stunumbers[i]);
            cell.SetCellType(1);
            cell.CellStyle = style1;
            row = sheet.CreateRow((short)(i + 1));
            cell = row.CreateCell((short)1);
            cell.SetCellValue(stuNames[i]);
            cell.SetCellType(1);
            cell.CellStyle = style1;
            for (int j = 0; j < subjectids.Length; j++)
            {
                HSSFCellStyle style2 = wb.CreateCellStyle();
                style2.BorderLeft = (short)1;
                style2.BorderRight = (short)1;
                style2.BorderTop = (short)1;
                style2.BorderBottom = (short)1;
                style2.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                row = sheet.CreateRow((short)(i + 1));
                cell = row.CreateCell((short)(2 + j));
                cell.SetCellValue(scores[i * subjectids.Length + j] == -1 ? "" : "" + scores[i * subjectids.Length + j]);
                cell.CellStyle = style2;
            }
        }


        string tick = DateTime.Now.Ticks.ToString();
        string save_path = temp_path + "\\" + ci.ClsName + subjectNames[0] + "成绩.xls";
        System.Web.HttpContext.Current.Session["filepath"] = ci.ClsName + subjectNames[0] + "成绩.xls";


        FileStream file = new FileStream(save_path, FileMode.Create);
        wb.Write(file);
        file.Close();
        return ci.ClsName + subjectNames[0] + "成绩.xls";
    }
}
